package com.yonyou.pmclouds.safetyrectification.rectificationrecord.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetyrectification.acceptrecord.entity.AcceptRecordVO;
import com.yonyou.pmclouds.safetyrectification.acceptrecord.service.IAcceptRecordQuery;
import com.yonyou.pmclouds.safetyrectification.rectificationrecord.entity.RectificationRecordVO;
import com.yonyou.pmclouds.safetyrectification.rectificationrecord.mapper.RectiRecordMapper;
import com.yonyou.pmclouds.safetyrectification.rectificationrecord.service.IRectiRecordQuery;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author tanghuan
 * @date 2019/10/26
 */
@Service(interfaceClass = IRectiRecordQuery.class, version = ApplicationConsts.APPLICATION_VERSION)
public class RectiRecordQueryImpl implements IRectiRecordQuery {
    @Autowired
    private RectiRecordMapper mapper;
    @Autowired
    private PictureRemoteService pictureRemoteService;
    @Autowired
    private IAcceptRecordQuery acceptRecordQuery;

    @Override
    public RectificationRecordVO getById(String id) throws BusinessException {
        return mapper.getById(id);
    }

    @Override
    public List<RectificationRecordVO> getByPkBill(String pkBill) throws BusinessException {
        List<RectificationRecordVO> rectificationRecordList = mapper.getByPkBill(pkBill);
        Map<String, List<String>> pictureMap = queryPics(rectificationRecordList);

        // 查询验收记录
        Map<String, AcceptRecordVO> acceptRecordVOMap = acceptRecordQuery.getByPkBill(pkBill);

        String pkRectificationRecord;
        for (RectificationRecordVO recordVO : rectificationRecordList) {
            pkRectificationRecord = recordVO.getPkRectificationRecord();
            recordVO.setAcceptRecordVO(acceptRecordVOMap.get(pkRectificationRecord));
            List<String> pictures = pictureMap.get(pkRectificationRecord);
            if (CollectionUtils.isNotEmpty(pictures))
                recordVO.setPictures(pictures.toArray(new String[0]));
        }

        return rectificationRecordList;
    }

    @Override
    public List<RectificationRecordVO> getByBillAndStatus(String pkBill, byte status) throws BusinessException {
        return mapper.getByBillAndStatus(pkBill, status);
    }

    /**
     * 查询整改记录的图片
     *
     * @param rectificationRecordList
     * @return
     * @throws BusinessException
     */
    private Map<String, List<String>> queryPics(List<RectificationRecordVO> rectificationRecordList) throws BusinessException {
        if (CollectionUtils.isEmpty(rectificationRecordList)) {
            return new HashMap<>();
        }

        Set<String> pkSet = new HashSet<String>();
        for (RectificationRecordVO recordVO : rectificationRecordList) {
            pkSet.add(recordVO.getPkRectificationRecord());
        }
        return pictureRemoteService.queryPicsByPkEntitys(pkSet.toArray(new String[0]));
    }

}
